package com.home.cca.adapters;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.home.cca.R;
import com.home.cca.model.Order;
import com.home.cca.util.DateUtil;

import java.util.Locale;

public class OrderViewHolder extends RecyclerView.ViewHolder {

    private final ImageView ivStatusIcon;
    private final TextView tvSchoolName;
    private final TextView tvBuyDate;
    private final TextView tvTotalMoney;
    private final TextView tvQty;

    public OrderViewHolder(View itemView) {
        super(itemView);
        ivStatusIcon = itemView.findViewById(R.id.ivStatusIcon);
        tvSchoolName = itemView.findViewById(R.id.tvSchoolName);
        tvBuyDate = itemView.findViewById(R.id.tvBuyDate);
        tvTotalMoney = itemView.findViewById(R.id.tvTotalMoney);
        tvQty = itemView.findViewById(R.id.tvQty);
    }

    public void bind(Order order, View.OnClickListener iconListener, View.OnClickListener nameListener, View.OnClickListener dateListener) {
        tvSchoolName.setText(String.format(Locale.getDefault(), "学校：%s", order.getSchoolName()));
        tvBuyDate.setText(String.format(Locale.getDefault(), "日期：%s", order.getBuyDate().substring(0, 16)));
        tvTotalMoney.setText(String.format(Locale.getDefault(), "金额：%.2f 元", order.getTotalMoney()));
        tvQty.setText(String.format(Locale.getDefault(), "数量：%.2f 斤", order.getMatList().stream().mapToDouble(Order.Material::getQty).sum()));
        ivStatusIcon.setImageResource(getStatus(order.getOrderState()));

        ivStatusIcon.setOnClickListener(iconListener);
        tvSchoolName.setOnClickListener(nameListener);
        tvBuyDate.setOnClickListener(dateListener);
        tvTotalMoney.setOnClickListener(dateListener);
        tvQty.setOnClickListener(dateListener);
    }

    private int getStatus(String orderState) {
        if ("待确认".equals(orderState)) {
            return R.drawable.icon_daiqueren;
        }
        if ("待发货".equals(orderState)) {
            return R.drawable.icon_daifahuo;
        }
        if ("待收货".equals(orderState)) {
            return R.drawable.icon_daishouhuo;
        }
        if ("已收货".equals(orderState)) {
            return R.drawable.icon_yishouhuo;
        }
        if ("已取消".equals(orderState)) {
            return R.drawable.icon_yiquxiao;
        }
        return 0;
    }

}